//
//  WWFindPasswordViewController.m
//  WeiWeiHaircut
//
//  Created by 超群 on 15/10/14.
//  Copyright © 2015年 超群. All rights reserved.
//

#import "WWFindPasswordViewController.h"
#import "WWRegisterCell.h"
#import "WWWritePasswordViewController.h"
#import "UIButton+timer.h"

@interface WWFindPasswordViewController () <UITableViewDelegate,UITableViewDataSource,QZRequestProtocol>
@property (strong,nonatomic)QZRequest *request;
@end

@implementation WWFindPasswordViewController{
    UIButton *_codeButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleWithSting:@"找回密码"];
    [self buildTableViewUI];
    
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidenKeyBoard)];
    [self.view addGestureRecognizer:tap];
    
    self.request = [[QZRequest alloc]init];
    self.request.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)buildTableViewUI
{
    self.tableView.delegate         = self;
    self.tableView.dataSource       = self;
    self.tableView.backgroundColor  = [UIColor groupTableViewBackgroundColor];

    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WWRegisterCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([WWRegisterCell class])];
}

- (void)hidenKeyBoard
{
    [self.view endEditing:YES];
}

#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

#pragma mark - tableView datasource
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WWRegisterCell * cell  = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WWRegisterCell class]) forIndexPath:indexPath];
    [cell.textField setTag:indexPath.row+1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.getCodeButton.hidden = NO;
        cell.titleLabel.text = @"手机号";
        cell.textField.keyboardType = UIKeyboardTypeNumberPad;
        
        [cell.getCodeButton addTarget:self action:@selector(getCodeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    if (indexPath.row == 1) {
        cell.titleLabel.text = @"验证码";
        cell.textField.keyboardType = UIKeyboardTypeNumberPad;
    }
    
    return cell;
}
#pragma mark- 获取验证码
-(void)getCodeAction:(UIButton *)btn
{
    _codeButton= btn;
    UITextField* textfield = (UITextField*)[self.view viewWithTag:1];
    if(![self checkTextfieldText:textfield]){
        [[iToast makeText:@"手机号不能为空"] show];
        return;
    }
    if (![self isValidateMobile:[textfield.text delSpace]]) {
        [[iToast makeText:@"请输入正确的手机号"] show];
        return;
    }
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",newPublicURL,@"User.GetSmsCode"];
    NSDictionary *dic = @{@"tel":[textfield.text delSpace]
                         };
    [self.request QZRequest_POST:urlStr parameters:dic tagNSString:@"User.GetSmsCode" stopRequest:NO isSerializer:NO isCache:NO];
    
   
}
#pragma mark- 提交信息
- (IBAction)submitTap:(UIButton* )sender {
    UITextField* textfield1 = (UITextField*)[self.view viewWithTag:1];
    UITextField* textfield2 = (UITextField*)[self.view viewWithTag:2];
    if (![self checkTextfieldText:textfield1]) {
        [[iToast makeText:@"手机号不能为空"] show];
        return;
    }
    if (![self checkTextfieldText:textfield2]) {
        [[iToast makeText:@"验证码不能为空"] show];
        return;
    }

    NSString *urlStr = [NSString stringWithFormat:@"%@%@",newPublicURL,@"User.GetSmsJc"];
    NSDictionary *dic = @{@"tel":[textfield1.text delSpace],
                          @"code" :[textfield2.text delSpace],
                          @"user_type":@(1)
                          };
    [self.request QZRequest_POST:urlStr parameters:dic tagNSString:@"User.GetSmsJc" stopRequest:NO isSerializer:NO isCache:NO];
}
#pragma  mark QZRequestProtocol
-(void)request_POST_FinishValue:(id)value tagNSString:(NSString *)tag
{
    

    if ([tag isEqualToString:@"User.GetSmsJc"]) {
        if ([value[@"data"][@"status"] intValue] == 100) {
//            UITextField* textField = (UITextField*)[self.view viewWithTag:1];
            WWWritePasswordViewController* writeVC = [[WWWritePasswordViewController alloc]initWithNibName:nil bundle:nil];
            writeVC.userName = value[@"data"][@"data"][@"user_id"];
            [self.navigationController pushViewController:writeVC animated:YES];
        }else{
            [[iToast makeText:@"验证码错误"] show];
        }
    }
    
    if ([tag isEqualToString:@"User.GetSmsCode"]){
        if ([value[@"data"][@"status"] intValue] == 100) {
            [[iToast makeText:@"验证码已发送"] show];
          [_codeButton addTimer];
        }else{
            [[iToast makeText:@"验证码获取失败"] show];
        }
    }
}
/*手机号码验证*/
-(BOOL) isValidateMobile:(NSString *)mobile
{
    NSString *phoneRegex = @"^1[3|4|5|7|8]\\d{9}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}
- (BOOL )checkTextfieldText:(UITextField*)textField
{
    NSString* firstrPassword = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([firstrPassword isEqualToString:@""]) {
        return NO;
    }else
        return YES;
}


@end
